package com.bocai.kelala.ui.travel.set.presenter;

import android.util.Log;

import com.bocai.kelala.bean.AddressListBean;
import com.bocai.kelala.bean.PageBean;
import com.bocai.kelala.ui.travel.set.contract.AboutAppContract;
import com.bocai.kelala.ui.travel.set.contract.AddAddressContract;
import com.bocai.kelala.util.AESUtils;
import com.bocai.kelala.util.RetryWithDelay;
import com.bocai.mybase.base.BaseResponse;
import com.bocai.mybase.net.Api;
import com.google.gson.Gson;

import javax.inject.Inject;

import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by yuanfei on 2017/7/13.
 */

public class AboutAppPresenter extends AboutAppContract.presenter {
    private final AboutAppContract.view view;
    private final Api api;
    public String start = "{\"content\":";
    public String end = "}";

    @Inject
    public AboutAppPresenter(AboutAppContract.view view, Api api) {
        this.view = view;
        this.api = api;
    }
    @Override
    public void webViewPage(String key) {
        view.showLoading("");
        api.webViewPage(key).subscribeOn(Schedulers.io()).retryWhen(new RetryWithDelay(3,3000))
                .observeOn(AndroidSchedulers.mainThread()).subscribe(new Subscriber<BaseResponse>() {
            @Override
            public void onCompleted() {
                view.stopLoading();
            }

            @Override
            public void onError(Throwable e) {
                Log.e("Throwable",e.toString());
                view.stopLoading();
                view.showErrorTip("网络异常");
            }

            @Override
            public void onNext(BaseResponse baseResponse) {
                view.stopLoading();
                Log.e("baseResponse",baseResponse.toString());
                if (baseResponse.getReturnCode().equals("200")){
                    String result = AESUtils.decode(baseResponse.getData());
                    Log.e("baseResponse",result);
                    PageBean bean = new Gson().fromJson(result,PageBean.class);
                    view.getData(bean);
                }else{
                    view.showErrorTip(baseResponse.getMsg());
                }
            }
        });
    }
}
